package org.service.impl;

import java.util.List;

import org.bean.O2rankgame;
import org.dao.GameDao;
import org.service.GameService;
import org.util.PageBean;

public class GameServiceImpl implements GameService {

	private O2rankgame O2rankgame;
	private GameDao GameDao;

	@SuppressWarnings("unchecked")
	public PageBean queryForGameForUid(int pageSize, int page, int uid) {
		final String hql;

		hql = "from O2rankgame g where g.uid=" + uid + " order by g.id desc ";

		int allRow = GameDao.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);

		List<GameDao> list = GameDao
				.queryForPageGameForUid(hql, offset, length);

		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;
	}

	@Override
	public O2rankgame SelectLastGameTimeByUid(int uid) {

		return this.GameDao.SelectLastGameTimeByUid(uid);
	}

	@Override
	public O2rankgame SelectByid(int id) {

		return this.GameDao.SelectByid(id);
	}

	@Override
	public List<O2rankgame> SelectUserHistoryCapable(int uid, String startTime,
			String endTime) {

		// final String hql =
		// "from O2rankgame g where  g.date between "+"'"+startTime+"'  and  "+"'"+endTime+"'   and   g.uid = "
		// + uid+" order by g.capable desc";

		final String hql = "select *  from O2RANKGAME where  date   between "
				+ "'"
				+ startTime
				+ "'  and  "
				+ "'"
				+ endTime
				+ "'  and  capable in (select max(capable) from O2RANKGAME where uid="
				+ uid + " group by Mid) and uid =" + uid
				+ " order by capable desc";

		List<O2rankgame> list = GameDao.SelectUserHistoryCapable(hql);

		return list;
	}

	@Override
	public List<O2rankgame> SelectTopCapable(int uid) {

		final String hql = "select *  from O2RANKGAME where capable in (select max(capable) from O2RANKGAME where uid="
				+ uid
				+ " group by Mid) and uid ="
				+ uid
				+ " order by capable desc";

		List<O2rankgame> list = GameDao.SelectTopCapable(hql);

		return list;
	}

	@Override
	public List<org.bean.O2rankgame> selectSongsTopByMid(int mid) {

		final String hql = "from O2rankgame  g where g.o2ranksongs.mid = "
				+ mid + "  order by g.score desc";

		return this.GameDao.selectSongsTopByMid(hql);
	}

	@Override
	public List<org.bean.O2rankgame> selectSongsTopByName(String name) {
		final String hql = "from O2rankgame  g where g.o2ranksongs.mname like '%"
				+ name + "%'  order by g.score desc";

		return this.GameDao.selectSongsTopByName(hql);
	}

	public O2rankgame getO2rankgame() {
		return O2rankgame;
	}

	public void setO2rankgame(O2rankgame o2rankgame) {
		O2rankgame = o2rankgame;
	}

	public GameDao getGameDao() {
		return GameDao;
	}

	public void setGameDao(GameDao gameDao) {
		GameDao = gameDao;
	}

}
